import axios from "axios";
import { Message } from "element-ui";
import router from "@/router";
//! 创建axios实例
let instance = axios.create({
  // baseURL: "https://www.liulongbin.top:8888/api/private/v1/",
  baseURL: "http://82.156.12.132:8080/api/private/v1/",
  timeout: "6000",
});
//! 设置请求拦截器 和 响应拦截器
instance.interceptors.request.use(
  (config) => {
    var userobj = sessionStorage.getItem("token");
    var token = "";
    if (userobj) {
      token = userobj;
    }
    config.headers.Authorization = token;
    //! 添加自己的配置
    return config;
  },
  (err) => {
    return Promise.reject(err);
  }
);
instance.interceptors.response.use(
  (res) => {
    if (res.data.meta.status == 404) {
      Message({
        message: "登录超时，请重新登陆",
        type: "warning",
      });
      console.log(router);
      // router.push('/login')
    }
    return res.data;
  },
  (err) => {
    return Promise.reject(err);
  }
);

export default instance;
